package clientView;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

import object.Specialized;
import utility.StringUtil;
import clientMain.Main;
import clientUtilGUI.UtilityGUI;
import database.SpecializedDelegate;

public class Spe_Update extends JDialog implements ActionListener{

	private JTextField txtSpeName;
	private JLabel lblSpeName;
	private JLabel lblSpeDes;
	private JTextArea txaSpeDes;
	private JButton btnSave;
	private JButton btnCancel;
	private JScrollPane jCsro;
	private int id;
	private int a;
	public String name;
	public String des;
	boolean flag = false;

	public Spe_Update(JFrame main, String spe_name, String mT, int id1,int row)  {
		super(main, "Sửa Chuyên Nghành", true);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		a = row;
		id = id1;
		name = spe_name;
		des = mT;
		int height = 200;
	    int width = 450;
	    setSize(width, height);
	    setLocation((UtilityGUI.screenSize.width-width)/2, (UtilityGUI.screenSize.height-height)/2);
	    setResizable(false);
	    initComponet();
	}
	
	   public void initComponet()
	   {	
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 1.0, 0.0};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 1.0};
		getContentPane().setLayout(gridBagLayout);
		
		lblSpeName = new JLabel("Chuyên Nghành: ");
		lblSpeName.setFont(UtilityGUI.fontLblForm);
		GridBagConstraints gbc_lblSpeName = new GridBagConstraints();
		gbc_lblSpeName.anchor = GridBagConstraints.WEST;
		gbc_lblSpeName.insets = new Insets(15, 15, 5, 5);
		gbc_lblSpeName.fill = GridBagConstraints.VERTICAL;
		gbc_lblSpeName.gridx = 0;
		gbc_lblSpeName.gridy = 0;
		getContentPane().add(lblSpeName, gbc_lblSpeName);
		
		txtSpeName = new JTextField();
		txtSpeName.setBackground(UtilityGUI.colorBgOldTxtUpdate);
		txtSpeName.setForeground(UtilityGUI.colorOldTxtUpdate);
		txtSpeName.setFont(UtilityGUI.fontTxtInsert);
		txtSpeName.setText(name);
		txtSpeName.addFocusListener(new FocusAdapter() {
			public void focusLost(FocusEvent e){
				txtSpeName.setText(StringUtil.removeUnwantedSpace(txtSpeName.getText()));
			}
		});
		txtSpeName.addFocusListener(new FocusListener() {
			
			@Override
			public void focusLost(FocusEvent e) {
				if(txtSpeName.getText().length()==0 || txtSpeName.getText() == null){
					txtSpeName.setText(name);
					txtSpeName.setBackground(UtilityGUI.colorBgOldTxtUpdate);
					txtSpeName.setForeground(UtilityGUI.colorOldTxtUpdate);
				}
			}			
			@Override
			public void focusGained(FocusEvent e) {
				if(txtSpeName.getText().equals(name)){
					txtSpeName.setBackground(UtilityGUI.colorBgNewTxtUpdate);
					txtSpeName.setFont(UtilityGUI.fontTxtInsert);
					txtSpeName.setText("");
					}
			}
		});
		//check database 
		txtSpeName.addCaretListener(new CaretListener() {
			public void caretUpdate(CaretEvent arg0) {
				SpecializedDelegate d = new SpecializedDelegate();
				if(!d.CheckInsertSpeName(StringUtil.removeUnwantedSpace(txtSpeName.getText()))&&txtSpeName.getText().length()>3){
					btnSave.setEnabled(true);
				}else{
					btnSave.setEnabled(false);
					
				}
			
			}
		});
		GridBagConstraints gbc_textField = new GridBagConstraints();
		gbc_textField.gridwidth = 2;
		gbc_textField.insets = new Insets(15, 5, 5, 15);
		gbc_textField.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField.gridx = 1;
		gbc_textField.gridy = 0;
		getContentPane().add(txtSpeName, gbc_textField);
		txtSpeName.setColumns(10);
		
		lblSpeDes = new JLabel("Mô tả: ");
		lblSpeDes.setFont(UtilityGUI.fontLblForm);
		GridBagConstraints gbc_lblSpeDes = new GridBagConstraints();
		gbc_lblSpeDes.anchor = GridBagConstraints.WEST;
		gbc_lblSpeDes.insets = new Insets(5, 15, 5, 5);
		gbc_lblSpeDes.fill = GridBagConstraints.VERTICAL;
		gbc_lblSpeDes.gridx = 0;
		gbc_lblSpeDes.gridy = 1;
		getContentPane().add(lblSpeDes, gbc_lblSpeDes);
		
		txaSpeDes = new JTextArea(5,20);
		txaSpeDes.setBackground(UtilityGUI.colorBgOldTxtUpdate);
		txaSpeDes.setForeground(UtilityGUI.colorOldTxtUpdate);
		txaSpeDes.setFont(UtilityGUI.fontTxtInsert);
		txaSpeDes.setTabSize(10);
		txaSpeDes.append(des);
		//add event
		txaSpeDes.addFocusListener(new FocusListener() {
			
			@Override
			public void focusLost(FocusEvent e) {
				String a =  "  ";
				a = a.trim();
				if(txaSpeDes.getText().trim().equals(a)){
					txaSpeDes.setText(des);
					txaSpeDes.setBackground(UtilityGUI.colorBgOldTxtUpdate);
					txaSpeDes.setForeground(UtilityGUI.colorOldTxtUpdate);
				}
			}			
			@Override
			public void focusGained(FocusEvent e) {
				if(txaSpeDes.getText().equals(des)){
					txaSpeDes.setBackground(UtilityGUI.colorBgNewTxtUpdate);
					txaSpeDes.setFont(UtilityGUI.fontTxtInsert);
					txaSpeDes.setText("");
					}
			}
		});
		jCsro = new JScrollPane(txaSpeDes);
		GridBagConstraints gbc_textArea = new GridBagConstraints();
		gbc_textArea.gridwidth = 2;
		gbc_textArea.insets = new Insets(5, 5, 5, 15);
		gbc_textArea.fill = GridBagConstraints.BOTH;
		gbc_textArea.gridx = 1;
		gbc_textArea.gridy = 1;
		getContentPane().add(jCsro, gbc_textArea);
		
		btnSave = new JButton("Lưu");
		btnSave.setEnabled(false);
		btnSave.setFont(UtilityGUI.fontBtnForm);
		GridBagConstraints gbc_btnSave = new GridBagConstraints();
		gbc_btnSave.fill = GridBagConstraints.VERTICAL;
		gbc_btnSave.anchor = GridBagConstraints.EAST;
		gbc_btnSave.insets = new Insets(10, 0, 5, 5);
		gbc_btnSave.gridx = 1;
		gbc_btnSave.gridy = 2;
		getContentPane().add(btnSave, gbc_btnSave);
		btnSave.addActionListener(this);
		
		btnCancel = new JButton("Thoát");
		btnCancel.setFont(UtilityGUI.fontBtnForm);
		GridBagConstraints gbc_btnCancel = new GridBagConstraints();
		gbc_btnCancel.fill = GridBagConstraints.BOTH;
		gbc_btnCancel.insets = new Insets(10, 0, 5, 15);
		gbc_btnCancel.gridx = 2;
		gbc_btnCancel.gridy = 2;
		getContentPane().add(btnCancel, gbc_btnCancel);
		btnCancel.addActionListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource()==btnSave){
			
			Specialized con = new Specialized();
			con.setSpecId(id);
			con.setSpecName(StringUtil.removeUnwantedSpace(txtSpeName.getText()));	
			con.setSpecDes(StringUtil.removeUnwantedSpace(txaSpeDes.getText().trim() ));
			SpecializedDelegate p = new SpecializedDelegate();
		    Boolean flag = p.updateSpe(con, Main.userId);
				 if(flag == true){
				    	            this.setVisible(false);
				    				JOptionPane.showMessageDialog(null, UtilityGUI.successAction, 
									UtilityGUI.titleAlert, JOptionPane.INFORMATION_MESSAGE);
						            Spe_View.tblSpe.getModel().setValueAt(StringUtil.removeUnwantedSpace(txtSpeName.getText()), a, 1) ;
						            Spe_View.tblSpe.getModel().setValueAt(StringUtil.removeUnwantedSpace(txaSpeDes.getText()), a, 2) ;
				  }else{
				    	 		JOptionPane.showMessageDialog(null, UtilityGUI.errorAction ,
								UtilityGUI.titleAlert, JOptionPane.ERROR_MESSAGE);
				    }
		}else if(e.getSource()==btnCancel){
			dispose();
		}
}



}
